//
//  CryptorProcessor.swift
//  chat
//
//  Created by 崔世强 on 2024/7/8.
//  Copyright © 2024 崔世强. All rights reserved.
//

import Foundation
import Kingfisher

struct CryptorProcessor: ImageProcessor {

    // `identifier` should be the same for processors with the same properties/functionality
    // It will be used when storing and retrieving the image to/from cache.
    let identifier = "me.shiqiang.CryptoProcessor"
    
    // Convert input data/image to target image and return it.
    func process(item: ImageProcessItem, options: KingfisherParsedOptionsInfo) -> KFCrossPlatformImage? {
        switch item {
        case .image(let image):
            // A previous processor already converted the image to an image object.
            // You can do whatever you want to apply to the image and return the result.
            return image
        case .data(let data):
            do {
                let decryptedData = try Cipher.shared.decrypt(data: data)
                return DefaultImageProcessor().process(item: .data(decryptedData), options: options)
            } catch {
                print("decrypt image error")
                return nil
            }
        }
    }
}
